from pydantic import BaseModel, Field, ConfigDict
from typing import Optional
from datetime import datetime


class BannerCreate(BaseModel):
    """
    创建轮播图的请求模型
    """
    title: str
    image_url: str
    link_url: Optional[str] = None
    description: Optional[str] = None
    sort_order: int = 0
    is_active: bool = True
    enable_time: Optional[datetime] = None


class BannerUpdate(BaseModel):
    """
    更新轮播图的请求模型
    """
    title: Optional[str] = None
    image_url: Optional[str] = None
    link_url: Optional[str] = None
    description: Optional[str] = None
    sort_order: Optional[int] = None
    is_active: Optional[bool] = None
    enable_time: Optional[datetime] = None


class BannerResponse(BaseModel):
    """
    轮播图响应模型
    """
    id: int
    title: str
    image_url: str
    link_url: Optional[str]
    description: Optional[str]
    sort_order: int
    is_active: bool
    enable_time: str = Field(..., description="启用时间，格式：YYYY-MM-DD HH:MM:SS")
    create_time: str = Field(..., description="创建时间，格式：YYYY-MM-DD HH:MM:SS")
    update_time: str = Field(..., description="更新时间，格式：YYYY-MM-DD HH:MM:SS")

    model_config = ConfigDict(from_attributes=True)
        
    @classmethod
    def from_orm(cls, obj):
        """
        从 ORM 对象创建响应模型，并格式化时间字段
        """
        return cls(
            id=obj.id,
            title=obj.title,
            image_url=obj.image_url,
            link_url=obj.link_url,
            description=obj.description,
            sort_order=obj.sort_order,
            is_active=obj.is_active,
            enable_time=obj.enable_time.strftime("%Y-%m-%d %H:%M:%S") if obj.enable_time else "",
            create_time=obj.create_time.strftime("%Y-%m-%d %H:%M:%S") if obj.create_time else "",
            update_time=obj.update_time.strftime("%Y-%m-%d %H:%M:%S") if obj.update_time else ""
        )


class BannerListResponse(BaseModel):
    """
    轮播图列表响应模型
    """
    code: int
    msg: str
    data: list[BannerResponse]
    total: int
    page: int
    page_size: int


class BannerStatusUpdate(BaseModel):
    """
    轮播图状态更新模型
    """
    is_active: bool